package ru.vsu.cs.piit.vokod.DAO;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Галчонок
 */
public class HSessionFactory {
    private static final SessionFactory sessionFactory;
    private static Session session;

    static {
        try {
            // Create the HSessionFactory from standard (hibernate.cfg.xml)
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

//    public static SessionFactory getSessionFactory() {
//        return sessionFactory;
//    }

    public static Session getSession()
    {
        if (session == null)
            session=sessionFactory.openSession();
        return session;
    }
}
